************************************************************************************************************
***************** Occupation-based measure in addition: Growth rates in imports per sector *****************
************************************************************************************************************
clear
use "BHPS_merged.dta"

sort pid waven


************************ Merge sectoral import shock: match with d=X based on occupation in t-X *************

* create industry affiliation from jbsic92 to merge with industry level data 
capture drop jbsic92_formergeCH
clonevar jbsic92_formergeCH=jbsic92
replace jbsic92_formergeCH=1 if (jbsic92>=100 & jbsic92<=150) |  (jbsic92>=200 & jbsic92<=202)  // AGRICULTURE, HUNTING AND FORESTRY
replace jbsic92_formergeCH=2 if jbsic92>=500 & jbsic92<=502									    // FISHING
replace jbsic92_formergeCH=3 if jbsic92>=1000 & jbsic92<=1450                                   // MINING AND QUARRYING
replace jbsic92_formergeCH=4 if jbsic92>=1500 & jbsic92<=1600                                   // Manufacture of Food Products and Beverages and Tobacco
replace jbsic92_formergeCH=5 if jbsic92>=1700 & jbsic92<=1930                                   // Manufacture of Textiles, Wearing Apparel; Dressing and Dyeing of Fur, Tanning and Dressing of Leather; Manufacture of Handbags, Saddlery, Harness And Footwear
replace jbsic92_formergeCH=6 if jbsic92>=2000 & jbsic92<=2052                                   // Manufacture of Wood And Products of Wood And Cork, Except Furniture; Manufacture of Articles of Straw & Plaiting Materials
replace jbsic92_formergeCH=7 if jbsic92>=2100 & jbsic92<=2233                                   // Manufacture of Pulp, Paper & Paper Products, Publishing, Printing & Reproduction of Recorded Media
replace jbsic92_formergeCH=8 if jbsic92>=2300 & jbsic92<=2330                                   // Manufacture of Coke, Refined Petroleum Products & Nuclear Fuel
replace jbsic92_formergeCH=9 if jbsic92>=2400 & jbsic92<=2470                                   // Manufacture of basic chemicals, Manufacture of pesticides and other agro-chemical products, Manufacture of paint, varnish & similar coatings, printing inks & cs,Manufacture of soap and detergents, cleaning and polishing preparations, perfumes and toilet preparations, Manufacture of other chemical products, Manufacture of pharmaceuticals, medicinal chemicals, botanical products
replace jbsic92_formergeCH=10 if jbsic92>=2500 & jbsic92<=2524                                  // Manufacture of Rubber and Plastic Products
replace jbsic92_formergeCH=11 if jbsic92>=2600 & jbsic92<=2682                                  // Manufacture of Other Non-metallic Mineral Products
replace jbsic92_formergeCH=12 if jbsic92>=2700 & jbsic92<=2754                                  // Manufacture of Basic Metals
replace jbsic92_formergeCH=13 if jbsic92>=2800 & jbsic92<=2875                                  // Manufacture of fabricated metal products, except machinery & equipment
replace jbsic92_formergeCH=14 if jbsic92>=2900 & jbsic92<=2972                                  // Manufacture of Machinery and Equipment Not Elsewhere Classified
replace jbsic92_formergeCH=15 if jbsic92>=3000 & jbsic92<=3002                                  // Manufacture of office machinery and computers
replace jbsic92_formergeCH=16 if jbsic92>=3100 & jbsic92<=3162                                  // Manufacture of Electrical Machinery & Apparatus Not Elsewhere Classified
replace jbsic92_formergeCH=17 if jbsic92>=3200 & jbsic92<=3230                                  // Manufacture of Radio, Television, Communication Equipment & Apparatus
replace jbsic92_formergeCH=18 if jbsic92>=3300 & jbsic92<=3350                                  // Manufacture of Medical, Precision and Optical Instruments, Watches and Clocks
replace jbsic92_formergeCH=19 if jbsic92>=3400 & jbsic92<=3430                                  // Manufacture of Motor Vehicles, Trailers and Semi-trailers
replace jbsic92_formergeCH=20 if jbsic92>=3500 & jbsic92<=3550                                  // Manufacture of Other Transport Equipment
replace jbsic92_formergeCH=21 if jbsic92>=3600 & jbsic92<=3720                                  // Manufacture of Furniture; Manufacturing Not Elsewhere Classified

replace jbsic92_formergeCH=99 if jbsic92>=4000 & jbsic92<=9999                                  // Other activities
replace jbsic92_formergeCH=99 if jbsic92==-9 | jbsic92==-8 | jbsic92==-7                        // Sector missing


replace jbsic92_formergeCH=. if jbsic92==410 | jbsic92==802 | jbsic92==810 | jbsic92==853 | jbsic92==2560 | jbsic92==3021 | jbsic92==3284   /// weird codes that shouldn't exist -> to missing


tab jbsic92_formergeCH

sort pid waven

* merge sectoral import data 
forvalues i=1/10 	{ 
		gen jbsic92_formergeCH_l`i'=L`i'.jbsic92_formergeCH
				}

forvalues i=1/10 	{ 
		merge m:1 jbsic92_formergeCH_l`i' year using "Sector_imports.dta", keepusing(sector_real_imp_CH_gr_d`i' sector_imprat_CH_gr_d`i' sect_imp_incrperw_CH_d`i') nogenerate
				}
		
******** Regressions ********	

*** only sectors concerned

	* take neglog of sectoral growth rate
		gen sector_real_imp_CH_gr_d3_nl = sign(sector_real_imp_CH_gr_d3) * ln(abs(sector_real_imp_CH_gr_d3)+1)
		gen sector_real_imp_CH_gr_d4_nl = sign(sector_real_imp_CH_gr_d4) * ln(abs(sector_real_imp_CH_gr_d4)+1)
	
	* Regressions (2002 observations missing)
		global controls "i.education i.male c.age_cent##c.age_cent i.bornelsewhere2 i.parentsbornelsewhere"
		
		sort pid waven
		
	* EU
		mixed D_eu_ordfac c.LDVeu_ordfactor ///
						real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_nl ///
						$controls i.ID_NUTS1xYear ///
						if (year==2006) ///
						|| ID_NUTS3xYear: , stddeviations 
		add_scalars_mixed
		eststo EU1
		sum D_eu_ordfac real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_nl if e(sample)

	
	* NAT
		mixed D3_nat_ordfactor L3.nat_ordfactor  ///
						real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_nl ///
						$controls i.ID_NUTS1xYear ///
						if (year==2008 | year==2005) ///
						|| ID_NUTS3: || ID_NUTS3xYear: , stddeviations 
		add_scalars_mixed_2level
		eststo NAT1
		sum D3_nat_ordfactor real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_nl if e(sample)

*** all respondents
 * set import growth rates zero for all "others" (not [known to be] employed in one of the sectors for which we have data in t-X)
		clonevar sector_real_imp_CH_gr_d3_B=sector_real_imp_CH_gr_d3
		clonevar sector_real_imp_CH_gr_d4_B=sector_real_imp_CH_gr_d4

		replace sector_real_imp_CH_gr_d3_B=0 if (jbsic92_formergeCH_l3<1 | jbsic92_formergeCH_l3==99) & ( (year==2005) | (year==2008) )
		replace sector_real_imp_CH_gr_d4_B=0 if (jbsic92_formergeCH_l4<1 | jbsic92_formergeCH_l4==99) & (year==2006)
				
		* take neglog of sectoral growth rate
		gen sector_real_imp_CH_gr_d3_B_nl = sign(sector_real_imp_CH_gr_d3_B) * ln(abs(sector_real_imp_CH_gr_d3_B)+1)
		gen sector_real_imp_CH_gr_d4_B_nl = sign(sector_real_imp_CH_gr_d4_B) * ln(abs(sector_real_imp_CH_gr_d4_B)+1)		
		
		* Regressions (2002 observations missing)
		global controls "i.education i.male c.age_cent##c.age_cent i.bornelsewhere2 i.parentsbornelsewhere"
		
		mixed D_eu_ordfac c.LDVeu_ordfactor ///
						real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_B_nl ///
						$controls i.ID_NUTS1xYear ///
						if (year==2006) ///
						|| ID_NUTS3xYear: , stddeviations reml
		add_scalars_mixed
		eststo EU2
		sum D_eu_ordfac real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_B_nl if e(sample)
		
		mixed D3_nat_ordfactor L3.nat_ordfactor  ///
						real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_B_nl ///
						$controls i.ID_NUTS1xYear ///
						if (year==2008 | year==2005) ///
						|| ID_NUTS3: || ID_NUTS3xYear: , stddeviations 
		add_scalars_mixed_2level
		eststo NAT2
		sum D3_nat_ordfactor real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_B_nl if e(sample)

					



************************ Merge sectoral import shock: match based on occupation in t *************
clear
use "BHPS_merged.dta"

sort pid waven


* create industry affiliation from jbsic92 to merge with industry level data 
capture drop jbsic92_formergeCH
clonevar jbsic92_formergeCH=jbsic92
replace jbsic92_formergeCH=1 if (jbsic92>=100 & jbsic92<=150) |  (jbsic92>=200 & jbsic92<=202)  // AGRICULTURE, HUNTING AND FORESTRY
replace jbsic92_formergeCH=2 if jbsic92>=500 & jbsic92<=502									    // FISHING
replace jbsic92_formergeCH=3 if jbsic92>=1000 & jbsic92<=1450                                   // MINING AND QUARRYING
replace jbsic92_formergeCH=4 if jbsic92>=1500 & jbsic92<=1600                                   // Manufacture of Food Products and Beverages and Tobacco
replace jbsic92_formergeCH=5 if jbsic92>=1700 & jbsic92<=1930                                   // Manufacture of Textiles, Wearing Apparel; Dressing and Dyeing of Fur, Tanning and Dressing of Leather; Manufacture of Handbags, Saddlery, Harness And Footwear
replace jbsic92_formergeCH=6 if jbsic92>=2000 & jbsic92<=2052                                   // Manufacture of Wood And Products of Wood And Cork, Except Furniture; Manufacture of Articles of Straw & Plaiting Materials
replace jbsic92_formergeCH=7 if jbsic92>=2100 & jbsic92<=2233                                   // Manufacture of Pulp, Paper & Paper Products, Publishing, Printing & Reproduction of Recorded Media
replace jbsic92_formergeCH=8 if jbsic92>=2300 & jbsic92<=2330                                   // Manufacture of Coke, Refined Petroleum Products & Nuclear Fuel
replace jbsic92_formergeCH=9 if jbsic92>=2400 & jbsic92<=2470                                   // Manufacture of basic chemicals, Manufacture of pesticides and other agro-chemical products, Manufacture of paint, varnish & similar coatings, printing inks & cs,Manufacture of soap and detergents, cleaning and polishing preparations, perfumes and toilet preparations, Manufacture of other chemical products, Manufacture of pharmaceuticals, medicinal chemicals, botanical products
replace jbsic92_formergeCH=10 if jbsic92>=2500 & jbsic92<=2524                                  // Manufacture of Rubber and Plastic Products
replace jbsic92_formergeCH=11 if jbsic92>=2600 & jbsic92<=2682                                  // Manufacture of Other Non-metallic Mineral Products
replace jbsic92_formergeCH=12 if jbsic92>=2700 & jbsic92<=2754                                  // Manufacture of Basic Metals
replace jbsic92_formergeCH=13 if jbsic92>=2800 & jbsic92<=2875                                  // Manufacture of fabricated metal products, except machinery & equipment
replace jbsic92_formergeCH=14 if jbsic92>=2900 & jbsic92<=2972                                  // Manufacture of Machinery and Equipment Not Elsewhere Classified
replace jbsic92_formergeCH=15 if jbsic92>=3000 & jbsic92<=3002                                  // Manufacture of office machinery and computers
replace jbsic92_formergeCH=16 if jbsic92>=3100 & jbsic92<=3162                                  // Manufacture of Electrical Machinery & Apparatus Not Elsewhere Classified
replace jbsic92_formergeCH=17 if jbsic92>=3200 & jbsic92<=3230                                  // Manufacture of Radio, Television, Communication Equipment & Apparatus
replace jbsic92_formergeCH=18 if jbsic92>=3300 & jbsic92<=3350                                  // Manufacture of Medical, Precision and Optical Instruments, Watches and Clocks
replace jbsic92_formergeCH=19 if jbsic92>=3400 & jbsic92<=3430                                  // Manufacture of Motor Vehicles, Trailers and Semi-trailers
replace jbsic92_formergeCH=20 if jbsic92>=3500 & jbsic92<=3550                                  // Manufacture of Other Transport Equipment
replace jbsic92_formergeCH=21 if jbsic92>=3600 & jbsic92<=3720                                  // Manufacture of Furniture; Manufacturing Not Elsewhere Classified

replace jbsic92_formergeCH=99 if jbsic92>=4000 & jbsic92<=9999                                  // Other activities
replace jbsic92_formergeCH=99 if jbsic92==-9 | jbsic92==-8 | jbsic92==-7                        // Sector missing


replace jbsic92_formergeCH=. if jbsic92==410 | jbsic92==802 | jbsic92==810 | jbsic92==853 | jbsic92==2560 | jbsic92==3021 | jbsic92==3284   /// weird codes that shouldn't exist -> to missing


tab jbsic92_formergeCH

sort pid waven

* merge sectoral import data 
forvalues i=1/10 	{ 
		merge m:1 jbsic92_formergeCH year using "Sector_imports.dta", keepusing(sector_real_imp_CH_gr_d`i' sector_imprat_CH_gr_d`i' sect_imp_incrperw_CH_d`i') nogenerate
				}
		
******** Regressions ********	

*** only sectors concerned

	* take neglog of sectoral growth rate
		gen sector_real_imp_CH_gr_d3_nl = sign(sector_real_imp_CH_gr_d3) * ln(abs(sector_real_imp_CH_gr_d3)+1)
		gen sector_real_imp_CH_gr_d4_nl = sign(sector_real_imp_CH_gr_d4) * ln(abs(sector_real_imp_CH_gr_d4)+1)
	
	* Regressions (2002 observations missing)
		global controls "i.education i.male c.age_cent##c.age_cent i.bornelsewhere2 i.parentsbornelsewhere"
		
		sort pid waven
		
	* EU
		mixed D_eu_ordfac c.LDVeu_ordfactor##i.year ///
						real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_nl ///
						$controls i.ID_NUTS1xYear ///
						if (year==2006 | year==2002) ///
						|| ID_NUTS3: || ID_NUTS3xYear: , stddeviations 
		add_scalars_mixed_2level
		eststo EU3
		sum D_eu_ordfac real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_nl if e(sample)

	
	* NAT
		mixed D3_nat_ordfactor L3.nat_ordfactor  ///
						real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_nl ///
						$controls i.ID_NUTS1xYear ///
						if (year==2008 | year==2005 | year==2002) ///
						|| ID_NUTS3: || ID_NUTS3xYear: , stddeviations 
		add_scalars_mixed_2level
		eststo NAT3
		sum D3_nat_ordfactor real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_nl if e(sample)

*** all respondents
 * set import growth rates zero for all "others" (not [known to be] employed in one of the sectors for which we have data in t-X)
		clonevar sector_real_imp_CH_gr_d3_B=sector_real_imp_CH_gr_d3
		clonevar sector_real_imp_CH_gr_d4_B=sector_real_imp_CH_gr_d4

		replace sector_real_imp_CH_gr_d3_B=0 if (jbsic92_formergeCH<1 | jbsic92_formergeCH==99) & ( (year==2002) | (year==2005) | (year==2008) )
		replace sector_real_imp_CH_gr_d4_B=0 if (jbsic92_formergeCH<1 | jbsic92_formergeCH==99) & ( (year==2002) | (year==2006) )
		
		
		* take neglog of sectoral growth rate
		gen sector_real_imp_CH_gr_d3_B_nl = sign(sector_real_imp_CH_gr_d3_B) * ln(abs(sector_real_imp_CH_gr_d3_B)+1)
		gen sector_real_imp_CH_gr_d4_B_nl = sign(sector_real_imp_CH_gr_d4_B) * ln(abs(sector_real_imp_CH_gr_d4_B)+1)		
		
		* Regressions (2002 observations missing)
		global controls "i.education i.male c.age_cent##c.age_cent i.bornelsewhere2 i.parentsbornelsewhere"
		
		mixed D_eu_ordfac c.LDVeu_ordfactor##i.year ///
						real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_B_nl ///
						$controls i.ID_NUTS1xYear ///
						if (year==2006 | year==2002) ///
						|| ID_NUTS3: || ID_NUTS3xYear: , stddeviations reml
		add_scalars_mixed_2level
		eststo EU4
		sum D_eu_ordfac real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_B_nl if e(sample)
		
		mixed D3_nat_ordfactor L3.nat_ordfactor  ///
						real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_B_nl ///
						$controls i.ID_NUTS1xYear ///
						if (year==2008 | year==2005 | year==2002) ///
						|| ID_NUTS3: || ID_NUTS3xYear: , stddeviations 
		add_scalars_mixed_2level
		eststo NAT4
		sum D3_nat_ordfactor real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_B_nl if e(sample)
	
	
	
	
	
************************ Write regression tables *************

		esttab EU1 EU2 EU3 EU4 using "Output tables\EU_withoccupation.rtf", b(a2) se(a2) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
						scalars(group1N group2N group3N ri1_std ri2_std ri3_std icc1 icc2 icc3 bic) varwidth(30) nogaps compress ///
						drop(0.male 0.education 0.bornelsewhere2 0.parentsbornelsewhere *.ID_NUTS1xYear lnsig_e:_cons) label replace /// 
						transform(ln*: exp(@) exp(@)) 						
					
	
		esttab NAT1 NAT2 NAT3 NAT4 using "Output tables\NAT_withoccupation.rtf", b(a2) se(a2) star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
						scalars(group1N group2N group3N ri1_std ri2_std ri3_std icc1 icc2 icc3 bic) varwidth(30) nogaps compress ///
						drop(0.male 0.education 0.bornelsewhere2 0.parentsbornelsewhere *.ID_NUTS1xYear lnsig_e:_cons) label replace /// 
						transform(ln*: exp(@) exp(@)) 			
					
					
		egen SectorYear= group(year jbsic92_formergeCH)
						
						|| _all: R.ID_NUTS3xYear || SectorYear: , stddeviations reml
						
						
						reml
						
						
						 ID_NUTS3: || : 
		add_scalars_mixed_2level
		eststo m1
		
					sum real_gr_imp_CH_d3_nl sector_real_imp_CH_gr_d3_nl if e(sample)
		


					sum real_gr_imp_CH_d4_nl sector_real_imp_CH_gr_d4_nl if e(sample)
	
	
		
		

		
				

************ MERGE INDUSTRY LEVEL IMPORT COMPETITION DATA FROM MARK SCHÄFER ************
**************************************************************************************		

/*
**************** Old data with all imports
* create industry affiliation from jbsic92 to merge with Mark Schäfer's industry level data (from 6 July 2018)
capture drop jbsic92_formerge
clonevar jbsic92_formerge=jbsic92
replace jbsic92_formerge=100 if jbsic92>=100 & jbsic92<=150
replace jbsic92_formerge=200 if jbsic92>=200 & jbsic92<=202
replace jbsic92_formerge=500 if jbsic92>=500 & jbsic92<=502
replace jbsic92_formerge=1000 if jbsic92>=1000 & jbsic92<=1030
replace jbsic92_formerge=1100 if jbsic92>=1100 & jbsic92<=1120
replace jbsic92_formerge=1710 if jbsic92>=1710 & jbsic92<=1717
replace jbsic92_formerge=1720 if jbsic92>=1720 & jbsic92<=1725
replace jbsic92_formerge=3700 if jbsic92>=3700 & jbsic92<=3720
replace jbsic92_formerge=4000 if jbsic92>=4000 & jbsic92<=4030
replace jbsic92_formerge=4500 if jbsic92>=4500 & jbsic92<=4550
replace jbsic92_formerge=5000 if jbsic92>=5000 & jbsic92<=5050
replace jbsic92_formerge=5100 if jbsic92>=5100 & jbsic92<=5170
replace jbsic92_formerge=5200 if (jbsic92>=5200 & jbsic92<=5212) | (jbsic92>=5220 &  jbsic92<=5274)
replace jbsic92_formerge=5500 if (jbsic92>=5500 & jbsic92<=5530) | jbsic92==5540 | (jbsic92>=5550 &  jbsic92<=5552)
replace jbsic92_formerge=6000 if jbsic92>=6000 & jbsic92<=6030
replace jbsic92_formerge=6100 if jbsic92==6110 & jbsic92==6120
replace jbsic92_formerge=6200 if jbsic92==6210 & jbsic92==6220
replace jbsic92_formerge=6300 if (jbsic92==6300 & jbsic92<=6323) | jbsic92==6330 | jbsic92==6340
replace jbsic92_formerge=6400 if jbsic92>=6400 & jbsic92<=6420
replace jbsic92_formerge=6500 if jbsic92>=6500 & jbsic92<=6523
replace jbsic92_formerge=6600 if jbsic92>=6600 & jbsic92<=6603
replace jbsic92_formerge=6700 if jbsic92>=6700 & jbsic92<=6720
replace jbsic92_formerge=7000 if jbsic92>=7000 & jbsic92<=7032
replace jbsic92_formerge=7100 if jbsic92>=7100 & jbsic92<=7140
replace jbsic92_formerge=7200 if jbsic92>=7200 & jbsic92<=7260
replace jbsic92_formerge=7300 if jbsic92>=7300 & jbsic92<=7320
replace jbsic92_formerge=7400 if (jbsic92>=7400 & jbsic92<=7460) | (jbsic92>=7470 &  jbsic92<=7484)
replace jbsic92_formerge=7500 if jbsic92>=7500 & jbsic92<=7530
replace jbsic92_formerge=8000 if jbsic92==8010 & jbsic92==8020 & jbsic92==8021 & jbsic92==8022 & jbsic92==8030 & jbsic92==8040 & jbsic92==8041 & jbsic92==8042
replace jbsic92_formerge=8500 if (jbsic92>=8500 & jbsic92<=8514) | (jbsic92>=8520 &  jbsic92<=8532)
replace jbsic92_formerge=9100 if jbsic92>=9100 & jbsic92<=9133

	// this deals restrictively with odd codes that are not mentioned in the codebook
	// i.e. they are all not used - rather than taken as falling in the higher level digit category
	// there are no data in MS' file for:
			* 9200 RECREATIONAL, CULTURAL AND SPORTING ACTIVITIES
			* 9300 Other service activities
			* 9900 EXTRA-TERRITORIAL ORGANISATIONS AND BODIES

	merge m:1 jbsic92_formerge year using "impshock_ind_MS_20181109.dta", gen(merge_ind_impshockMS)

*/
